-
Notifications
You must be signed in to change notification settings - Fork 98
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: allow unpaginated request #406
Conversation
Codecov ReportBase: 97.08% // Head: 97.19% // Increases project coverage by
Additional details and impacted files@@ Coverage Diff @@
## master #406 +/- ##
==========================================
+ Coverage 97.08% 97.19% +0.11%
==========================================
Files 6 7 +1
Lines 274 285 +11
Branches 92 96 +4
==========================================
+ Hits 266 277 +11
Misses 8 8
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. ☔ View full report at Codecov. |
fyi discussion in #393 |
+ test on limit
README.md
Outdated
@@ -199,7 +199,7 @@ const paginateConfig: PaginateConfig<CatEntity> { | |||
* Required: false | |||
* Type: number | |||
* Default: 100 | |||
* Description: The maximum amount of entities to return per page. | |||
* Description: The maximum amount of entities to return per page. Set it to 0, in conjunction with limit=0 on query param, to disable pagination. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
place addition in next line, as it's a code frame with horizontal slider
src/paginate.ts
Outdated
export const DEFAULT_LIMIT = 20 | ||
export const NO_PAGINATION = 0 | ||
|
||
const positiveNumberOrDefault = (value: number | undefined, defaultValue: number, minValue: 0 | 1 = 0) => |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move to helper.ts
src/paginate.ts
Outdated
.skip((page - 1) * limit) | ||
} else { | ||
queryBuilder = repo.take(limit).skip((page - 1) * limit) | ||
if (limit) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use isPaginated
src/paginate.ts
Outdated
@@ -362,9 +370,6 @@ export async function paginate<T extends ObjectLiteral>( | |||
|
|||
;[items, totalItems] = await queryBuilder.getManyAndCount() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does this still execute 2 queries ? (1) count and (2) find?
should be then just using getMany()
. no need to count if you have no limit.
src/paginate.ts
Outdated
} else { | ||
results = { | ||
data: items, | ||
meta: { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's just branch the calculation of the meta values which are computed differently now, instead of this larger code duplication
const limit = Math.min(query.limit || config.defaultLimit || 20, config.maxLimit || 100) | ||
const page = positiveNumberOrDefault(query.page, 1, 1) | ||
|
||
const defaultLimit = config.defaultLimit || DEFAULT_LIMIT |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm, it missed my comment here. here again:
let's untangle this a bit and add isPaginated
for readablity
const defaultLimit = config.defaultLimit || DEFAULT_LIMIT
const maxLimit = positiveNumberOrDefault(config.maxLimit, DEFAULT_MAX_LIMIT, 0)
const queryLimit = positiveNumberOrDefault(query.limit, defaultLimit, 0)
const isPaginated = !(queryLimit === NO_PAGINATION && maxLimit === NO_PAGINATION)
const limit = isPaginated ? Math.min(queryLimit, maxLimit || DEFAULT_MAX_LIMIT) : NO_PAGINATION
🎉 This PR is included in version 4.6.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
fix #393